﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Client;
using System.Collections;

namespace SnapShot
{
    public class WorkItemList
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }

    public static class TFS
    {
        public static string NomeServidor { get; set; }
        public static string ProjetoPadrao { get; set; }        

        static Project _projeto;
        public static Project Projeto
        {
            get
            {
                if (_projeto == null)
                {
                    TeamFoundationServer server = GetServer();
                    WorkItemStore store = new WorkItemStore(server);
                    
                    if (string.IsNullOrEmpty(ProjetoPadrao) == false)
                    {
                        _projeto = store.Projects[ProjetoPadrao];
                    }
                    else
                    {                        
                        _projeto = store.Projects[0];
                    }
                }
                return _projeto;
            }
        }

        private static TeamFoundationServer GetServer()
        {
            TeamFoundationServer server = null;

            server = new Microsoft.TeamFoundation.Client.TeamFoundationServer(NomeServidor);
            server.Authenticate();
            return server;
        }

        public static List<WorkItemList> ListaMeusWorkItens(string situacoes)
        {
            //Hashtable parameters = new Hashtable();
            //parameters.Add("State", "Em Teste");
            //parameters.Add("State", "Ativa");

            List<WorkItemList> wl = new List<WorkItemList>();
            WorkItemCollection wc = Projeto.Store.Query(String.Format("SELECT [System.Id], [System.Title] FROM WorkItems WHERE [System.AssignedTo] = @Me AND [System.State] IN ({0})", string.IsNullOrEmpty(situacoes) ? "'0'": situacoes));
            foreach(WorkItem wi in wc)
            {
                wl.Add(new WorkItemList(){Id = wi.Id, Title = wi.Title});
            }
            return wl;
        }

        internal static void RefazerLogon()
        {
            _projeto = null;
        }

        public static void GetFile()
        {
            //Projeto.
        }
    }
}
